package demo1;
/**
 * 弹夹类
 */
public class Clip {
    private Bullet[] magazine;//弹仓
    private int capacity = 30;//弹夹容量
    private int surplus=0;//子弹盈余
    public Clip(){
        this(30);
    }
    public Clip(int capacity){
        this.magazine = new Bullet[capacity];
        this.surplus = 0;
        //showClip();
    }
    //装子弹
    public void pushBullet(Bullet bullet){
        if(surplus == capacity){
            System.out.println(">>>弹夹已满,请勿重复装弹!");
            return;
        }
        magazine[surplus] = bullet;
        surplus++;
        //showClip();
    }
    //卸子弹
    public Bullet popBullet(){
        if(surplus == 0){
            System.out.println(">>>弹夹已空,无法弹出子弹!");
            return null;
        }
        Bullet bullet = magazine[surplus - 1];
        magazine[surplus - 1] = null;
        surplus--;
        showClip();
        return bullet;
    }
    //显示弹夹信息
    public void showClip(){
        System.out.printf(">>>弹夹状态:%d/%d\n",surplus,capacity);
    }
}

